﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Framework.Common.Extention
{
    class ResponseExtention
    {
        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strValue">值</param>
        public static void WriteCookie(string strName, string strValue, string domain = "")
        {
            HttpContext context = HttpContext.Current;
            HttpCookie cookie = context.Request.Cookies[strName] ?? new HttpCookie(strName);
            cookie.Value = strValue;
            if (!string.IsNullOrWhiteSpace(domain))
                cookie.Domain = domain;
            context.Response.AppendCookie(cookie);
        }

        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strValue">值</param>
        public static void WriteCookie(string strName, string key, string strValue, string domain = "")
        {
            HttpContext context = HttpContext.Current;
            HttpCookie cookie = context.Request.Cookies[strName] ?? new HttpCookie(strName);
            cookie[key] = strValue;
            if (!string.IsNullOrWhiteSpace(domain))
                cookie.Domain = domain;
            context.Response.AppendCookie(cookie);
        }

        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strValue">值</param>
        /// <param name="strValue">过期时间(分钟)</param>
        public static void WriteCookie(string strName, string strValue, int expires, string domain = "")
        {
            HttpContext context = HttpContext.Current;
            HttpCookie cookie = context.Request.Cookies[strName] ?? new HttpCookie(strName);
            cookie.Value = strValue;
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            if (!string.IsNullOrWhiteSpace(domain))
                cookie.Domain = domain;
            context.Response.AppendCookie(cookie);
        }
    }
}
